//
//  AttributedLabel.m
//  ZJWR
//
//  Created by myqu on 14-8-31.
//  Copyright (c) 2014年 3TI. All rights reserved.
//

#import "AttributedLabel.h"
#import "NSString+Utils.h"
@interface AttributedLabel(){
    UIFont *myFont;
}
@property (nonatomic,retain)NSMutableAttributedString          *attString;
@end

@implementation AttributedLabel

@synthesize attString = _attString;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)drawRect:(CGRect)rect
{
    
    for (CATextLayer * layer in self.layer.sublayers)
    {
        [layer removeFromSuperlayer];
    }
    
    CATextLayer *textLayer = [CATextLayer layer];
    textLayer.string = _attString;
    textLayer.transform = CATransform3DMakeScale(0.5,0.5,1);
    textLayer.frame = CGRectMake(0, self.textContentOrginY, self.frame.size.width, self.frame.size.height);
    [self.layer addSublayer:textLayer];
}

- (void)setText:(NSString *)text{
    [super setText:text];
    if (text == nil) {
        self.attString = nil;
    }else{
        self.attString = [[NSMutableAttributedString alloc] initWithString:text];
    }
    
    self.lineBreakMode = NSLineBreakByCharWrapping;
}

-(void)setFont:(UIFont *)font
{
    myFont = font;
    [_attString addAttribute:(NSString *)kCTFontAttributeName
                       value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
                                                                        font.pointSize*2,
                                                                        NULL))
                       range:NSMakeRange(0, self.text.length)];

}

// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTForegroundColorAttributeName
                       value:(id)color.CGColor
                       range:NSMakeRange(location, length)];
}

// 设置某段字的字体
- (void)setFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
    
    myFont = font;
    
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTFontAttributeName
                       value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
                                                                        font.pointSize*2,
                                                                        NULL))
                       range:NSMakeRange(location, length)];
}

// 设置某段字的风格
- (void)setStyle:(CTUnderlineStyle)style fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attString addAttribute:(NSString *)kCTUnderlineStyleAttributeName
                       value:(id)[NSNumber numberWithInt:style]
                       range:NSMakeRange(location, length)];
}


-(void)setMyTextAlignment:(NSTextAlignment)textAlignment
{
    CGSize size = [_attString.string sizeWithFont:myFont];
    switch (textAlignment) {
        case NSTextAlignmentCenter:
        {
            CGRect rect = self.frame;
            rect.origin.x =rect.origin.x + (rect.size.width-size.width)/2;
            rect.origin.y = rect.origin.y;
            rect.size.width = size.width+5;
            self.frame =rect;
        }
            break;
        case NSTextAlignmentRight:
        {
            CGRect rect = self.frame;
            rect.origin.x =rect.origin.x + (rect.size.width-size.width);
            rect.origin.y = rect.origin.y +(rect.size.height-size.height)/2;
            rect.size.width = size.width;
            self.frame =rect;
        }
            break;
        default:
            break;
    }
}

@end
